package com.shujia.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class Demo1TestJavaApi {
    public static void main(String[] args) throws IOException {
        //1、创建hbase链接
        //创建hadoop配置对象
        Configuration configuration = HBaseConfiguration.create();
        //指定链接参数
        //指定zk地址，在zk中保存了hbase元数据
        configuration.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
        //建立链接
        Connection connection = ConnectionFactory.createConnection(configuration);

        /*
         * 查询数据
         */

        //获取表对象
        Table student = connection.getTable(TableName.valueOf("student"));

        //构建get对象，指定rowkey
        Get get = new Get(Bytes.toBytes("001"));

        //查询数据，返回result
        Result result = student.get(get);

        //解析result,返回字节数组
        byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
        String name = Bytes.toString(value);
        System.out.println(name);

        //关闭链接
        connection.close();
    }
}
